<script setup>
import { ref } from 'vue';
import { useRouter } from 'vue-router';
import { User, Lock, Present } from '@element-plus/icons-vue';
 

const isRegister = ref(false)
 
const form = ref()
const formModel = ref({
  userName: '',
  password: '',
  checkpass:'',
  // 验证码
  authcode: '',
  email:''
})

// 整个表单的校验规则
const rules={
      userName: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 5, max: 10, message: '用户名必须是5-10位的字符', trigger: 'blur' }
      ],
      password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        {
          pattern: /^\S{6,15}$/,
          message: '密码必须是6-15位的非空字符',
          trigger: 'blur'
        }
      ],
      checkpass: [
        { required: true, message: '请再次输入密码', trigger: 'blur' },
        {
          pattern: /^\S{6,15}$/,
          message: '密码必须是6-15的非空字符',
          trigger: 'blur'
        },
        {
          validator: (rule, value, callback) => {
            if (value !== formModel.value.checkpass) {
              callback(new Error('两次输入密码不一致!'))
            } else {
              callback()
            }
          },
          trigger: 'blur'
        }
      ],
      email:[
        {
          required: true,
          message: '请输入邮箱',
          trigger: 'blur',
        },
        {
          type: 'email',
          message: '请输入正确邮箱',
          trigger: ['blur', 'change'],
        },
      ],
      authcode: [
          { required: true, message: '请输入邮箱验证码', trigger: 'blur' }
        ]

}


</script>

<template>
    <RouterView />
    <el-row class="login-page">
        <el-col :span="12" class="bg"></el-col>
        <el-col :span="6" :offset="3" class="form">
            


             <!-- 注册相关 -->
      <div v-if="isRegister">
      <el-form 
      :model="formModel"
      :rules="rules"
      ref="form" 
      size="large" 
      autocomplete="off" 
      v-if="isRegister"
      >
        <el-form-item>
          <h1>注册</h1>
        </el-form-item>
        <!-- 用户名 -->
        <el-form-item prop="userName">
          <el-input 
          v-model="formModel.userName"
          :prefix-icon="User" 
          placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <!-- 新密码注册 -->
        <el-form-item prop="password">
          <el-input
          v-model="formModel.password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <!-- 再次输入密码 -->
        <el-form-item prop="checkpass">
          <el-input
          v-model="formModel.checkpass"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入再次密码"
          ></el-input>
        </el-form-item>
      
        <!-- 邮箱-->
        <el-form-item prop="email">
          <el-input 
          v-model="formModel.email"
          :prefix-icon="Present" 
          placeholder="邮箱"
          ></el-input>
        </el-form-item>
             <!--邮箱验证码-->
            
        <div style="display: flex;margin-bottom: 20px;"> 
         <el-form-item label="" prop="authcode">
            <el-input v-model="formModel.authcode"></el-input>
        </el-form-item>
        <el-form-item>
         <el-button @click="sendVerificationCode" type="primary">发送验证码</el-button>
        </el-form-item>
         </div> 
       
 
        <el-form-item>
          <el-button
          @click="uregister"
          class="button" 
          type="primary" 
          auto-insert-space>
            注册
          </el-button>
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = false">
            返回
          </el-link>
        </el-form-item>
      </el-form>
    </div>


       <!-- 登陆相关 -->
    <el-form  
      :model="formModel"
      :rules="rules"
      ref="form" 
      size="large" 
      autocomplete="off" v-else
      >
        <el-form-item>
          <h1>登录</h1>
        </el-form-item>
        <el-form-item prop="email">
          <el-input
          v-model="formModel.email"
           :prefix-icon="User" 
           placeholder="请输入邮箱"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
          v-model="formModel.password"
            name="password"
            :prefix-icon="Lock"
            type="password"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
         
        <el-form-item>
          <el-button 

          @click="Login()"
          class="button" 
          type="primary" auto-insert-space
            >登录</el-button
          >
        </el-form-item>
        <el-form-item class="flex">
          <el-link type="info" :underline="false" @click="isRegister = true">
            注册 
          </el-link>
        </el-form-item>
      </el-form>

        </el-col>
    </el-row>
        


</template>

<style lang="scss" scoped>

.login-page{
    height: 100vh;
    background-color: white;
    .bg{
        background:  
        url('@/assets/images/1.jpg') no-repeat center / cover;
    }

    .form {
    display: flex;
    flex-direction: column;
    justify-content: center;
    user-select: none;
    .title {
      margin: 0 auto;
    }
    .button {
      width: 100%;
    }
    .flex {
      width: 100%;
      display: flex;
      justify-content: space-between;
    }
  }
}
</style>
